/**
 * 
 */
package com.shuqi.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuqi.activity.adapter.OfferWallTopAdapter;
import com.shuqi.activity.viewport.CommonTitle;
import com.shuqi.application.ShuqiApplication;
import com.shuqi.common.FunctionsSwitch;
import com.shuqi.controller.R;

/**
 * 推广墙activity
 * 
 * @author linyuren
 * 
 */
public class OfferWallTopActivity extends FragmentActivityBase implements OnClickListener {

    /** viewpaper */
    private ViewPager viewPager;

    private OfferWallTopAdapter mAdapter;

    /** 页卡选项索引 */
    private int curIndex = 0;
    private CommonTitle mTitle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.act_offerwalltop);
        this.init();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left:
                ShuqiApplication.getApplicationHandler().postDelayed(new Runnable() {
                    public void run() {
                        finishOfferWall();
                    }
                }, 300);
                
                break;
            case R.id.bt_hotapp_back:
            case R.id.bt_hotapp_text:
                switchTableStates(0, false);
                break;
            case R.id.bt_goodgame_back:
            case R.id.bt_goodgame_text:
                switchTableStates(1, false);
                break;
        }
    }
    
    private void finishOfferWall() {
        ActivityManager.getInstance().finishActivity(this);
    }

    /** 初始化 */
    private void init() {
        mTitle = (CommonTitle) findViewById(R.id.title);
        mTitle.setLeftClickListener(this);
        this.findViewById(R.id.bt_hotapp_back).setOnClickListener(this);
        this.findViewById(R.id.bt_hotapp_text).setOnClickListener(this);

        this.findViewById(R.id.bt_goodgame_back).setOnClickListener(this);
        this.findViewById(R.id.bt_goodgame_text).setOnClickListener(this);

        this.viewPager = (ViewPager) findViewById(R.id.offerwall_content_viewpaper);
        mAdapter = new OfferWallTopAdapter(getSupportFragmentManager(), this);
        viewPager.setAdapter(mAdapter);

        this.viewPager.setCurrentItem(this.curIndex);
        this.viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
        
        FunctionsSwitch.setOfferWallUpdate(this, false);
    }


    /** 切换标签改变选中状态 */
    private void switchTableStates(int index, boolean isSliding) {
        if (index == 0) {
            ((ImageView) findViewById(R.id.rec_selected)).setVisibility(View.VISIBLE);
            ((ImageView) findViewById(R.id.rank_selected)).setVisibility(View.GONE);
            ((TextView) findViewById(R.id.bt_goodgame_text)).setTextColor(getResources().getColor(
                    R.color.bookditails_tab_text_n));
            ((TextView) findViewById(R.id.bt_hotapp_text)).setTextColor(getResources().getColor(
                    R.color.bookditails_tab_text_f));
        } else {
            ((TextView) findViewById(R.id.bt_goodgame_text)).setTextColor(getResources().getColor(
                    R.color.bookditails_tab_text_f));
            ((TextView) findViewById(R.id.bt_hotapp_text)).setTextColor(getResources().getColor(
                    R.color.bookditails_tab_text_n));
            ((ImageView) findViewById(R.id.rank_selected)).setVisibility(View.VISIBLE);
            ((ImageView) findViewById(R.id.rec_selected)).setVisibility(View.GONE);
        }
        if (isSliding) {
            this.mAdapter.notifyOnPageSelected(index);
            return;
        }
        this.viewPager.setCurrentItem(index);
    }

    /**
     * 页卡切换监听
     */
    private class MyOnPageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageSelected(int arg0) {
            curIndex = arg0;
            switchTableStates(curIndex, true);
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            ActivityManager.getInstance().finishActivity(this);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
